﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace nMongo.Helpers
{
    public static class MongoCollectionHelper
    {
        public static string GetCollectionNameFromType(Type type)
        {
            string collectionName;
            object[] attributes = type.GetCustomAttributes(typeof(nMongo.MongoCollectionAttribute), false);
            if (attributes.Length == 0 || attributes.Count(x => x is nMongo.MongoCollectionAttribute) == 0)
            {
                collectionName = type.Name;
            }
            else
            {
                collectionName = ((MongoCollectionAttribute)attributes
                    .First(x => x is MongoCollectionAttribute)).CollectionName;
            }
            return collectionName;
        }

        public static string GetCollectionNameFromType<T>()
        {
            return MongoCollectionHelper.GetCollectionNameFromType(typeof(T));
        }
    }
}
